<template>
  <div class="swiper-container" ref="swiper" >
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="item in slideList" :key="item.id">
        <img :src="item.imgUrl" />
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
import Swiper from "swiper";
import "swiper/css/swiper.min.css";
export default {
  name: "Swiper",
  props: ["slideList"],
  methods: {
    //初始化 swiper
    swiperInit() {
      // $refs函数获取元素的真是dom
      new Swiper(this.$refs.swiper, {
        loop: true, // 循环播放
        // 如果需要分页
        pagination: {
          el: ".swiper-pagination",
        },

        // 如果需要前进后退按钮
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
      });
    },
  },
  watch: {
    slideList:{
      immediate:true,
      handler(){
        this.$nextTick(() => {
        //初始化swiper
          this.swiperInit();
        });
      },
      // this.$nextTick(回调函数)：将回调函数延迟到下次DOM更新循环之后执行，在修改数据之后立即使用它，然后更新dom
      // 一般在我们直接操作真实dom的时候要使用，因为数据更新之后，dom的更新需要时间，所以必须等dom更新之后才能操作真实dom
      
    },
  },
};
</script>

<style>
</style>